package com.project.beagentleman;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;

public class LessonsActivity extends Activity {

	private LinearLayout lessonsLL;
	private List<Lesson> lessonsList;

	private int buttonWidth;

	private View manV;
	private View womanV;
	private View generalV;

	private TextView manTV;
	private TextView womanTV;
	private TextView generalTV;

	private TextView lessonTitle;
	private LinedEditText lessonMan;
	private LinedEditText lessonWoman;
	private LinedEditText lessonGeneral;

	private Button lessonGeneralBT;


	private int lessonNumber = 0;
	private LinearLayout leckek1;
	private LinearLayout leckek_2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_lessons);
		

		initComponents();
	}
	
	public void initComponents() {
		
		buttonWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
		lessonsLL = (LinearLayout) findViewById(R.id.lessonsLL);
		leckek1 = (LinearLayout) findViewById(R.id.leckek);
		leckek_2 = (LinearLayout) findViewById(R.id.leckek2);


		manV = (View) findViewById(R.id.manV);
		womanV = (View) findViewById(R.id.womanV);
		generalV = (View) findViewById(R.id.generalV);

		manTV = (TextView) findViewById(R.id.manTV);
		womanTV = (TextView) findViewById(R.id.womanTV);
		generalTV = (TextView) findViewById(R.id.generalTV);

		lessonTitle = (TextView) findViewById(R.id.lessonTitleTV);
		lessonMan = (LinedEditText) findViewById(R.id.lessonManLET);
		lessonMan.setKeyListener(null);
		lessonWoman = (LinedEditText) findViewById(R.id.lessonWomanLET);
		lessonWoman.setKeyListener(null);
		lessonGeneral = (LinedEditText) findViewById(R.id.lessonGeneralLET);
		lessonGeneral.setKeyListener(null);

		lessonGeneralBT = (Button) findViewById(R.id.lessonGeneralBT);
		lessonGeneralBT.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(ImageActivity.class);
				ImageActivity.lessonNumber = lessonNumber;
				ImageActivity.lessonType = "g";
			}
		});

		parseLessonsXML();

		for (int i = 0; i < lessonsList.size(); i++) {
			final int k = i;
			
			final Button button = new Button(this);
			button.setText((i+1) + ". "+ getResources().getString(R.string.lesson));

			LayoutParams lp = new LayoutParams(buttonWidth, LayoutParams.WRAP_CONTENT);
			button.setLayoutParams(lp);
			button.setBackgroundResource(R.xml.button_selector);
			
			
			leckek1.setVisibility(View.GONE);
			leckek_2.setVisibility(View.VISIBLE);
			


			button.setOnClickListener(new OnClickListener() {
				
				
				@Override
				public void onClick(View v) {
					leckek1.setVisibility(View.VISIBLE);
					leckek_2.setVisibility(View.GONE);					
					lessonTitle.setText(lessonsList.get(k).getTitle());
					String tmpLesson = "";

					tmpLesson = lessonsList.get(k).getMan();
					if (!tmpLesson.isEmpty()) {
						lessonMan.setText(tmpLesson);
						setVisible(manV, manTV, lessonMan, View.VISIBLE);
					} else {
						setVisible(manV, manTV, lessonMan, View.GONE);
					}

					tmpLesson = lessonsList.get(k).getWoman();
					if (!tmpLesson.isEmpty()) {
						lessonWoman.setText(tmpLesson);
						setVisible(womanV, womanTV, lessonWoman, View.VISIBLE);
					} else {
						setVisible(womanV, womanTV, lessonWoman, View.GONE);
					}

					tmpLesson = lessonsList.get(k).getGeneral();
					if (!tmpLesson.isEmpty()) {
						lessonGeneral.setText(tmpLesson);
						setVisible(generalV, generalTV, lessonGeneral, View.VISIBLE);
					} else {
						setVisible(generalV, generalTV, lessonGeneral, View.GONE);
					}

					lessonNumber = k + 1;
				}
			});

			lessonsLL.addView(button);
		}
	}

	public void setVisible(View tmpV, TextView tmpTV, TextView lessonTmp, int type) {
		tmpV.setVisibility(type);
		tmpTV.setVisibility(type);
		lessonTmp.setVisibility(type);
	}

	public void parseLessonsXML() {
		Lesson lesson = null;
		String text = "";
		lessonsList = new ArrayList<Lesson>();		

		try {
			Resources resources = getResources();
			XmlResourceParser xmlResourceParser = resources.getXml(R.xml.lessons_hu);
			xmlResourceParser.next();

			int eventType = xmlResourceParser.getEventType();			

			while (eventType != XmlPullParser.END_DOCUMENT) {
				String tagname = xmlResourceParser.getName();

				switch (eventType) {

				case XmlPullParser.START_TAG:
					if (tagname.equalsIgnoreCase("lesson")) {
						lesson = new Lesson();
					}
					break;

				case XmlPullParser.TEXT:
					text = xmlResourceParser.getText();
					break;

				case XmlPullParser.END_TAG:
					if (tagname.equalsIgnoreCase("lesson")) {
						lessonsList.add(lesson);
					} else if (tagname.equalsIgnoreCase("title")) {
						lesson.setTitle(text);
					} else if (tagname.equalsIgnoreCase("man")) {
						lesson.setMan(text);
					} else if (tagname.equalsIgnoreCase("woman")) {
						lesson.setWoman(text);
					} else if (tagname.equalsIgnoreCase("general")) {
						lesson.setGeneral(text);
					}
					break;

				default:
					break;
				}
				eventType = xmlResourceParser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void startActivity(Class<?> activityClass) {
		startActivity(new Intent(this, activityClass));
	}
}
